﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sbook.Data;
using Sbook.Business;
using Sbook.Library;
namespace Sbook
{
    public partial class index : System.Web.UI.Page
    {
        protected string imgBanner1 = "images/banner1.png";
        protected string imgBanner2 = "images/banner-2.png";
        protected string imgBanner3 = "images/banner3.png";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Business.BannerService.Banner_GetByAll().FindAll(b => b.BType == "2" && b.BStatus == "1").Count > 0)
                {
                    imgBanner1 = Business.BannerService.Banner_GetByAll().FindAll(b => b.BType == "2" && b.BStatus == "1")[0].BLink;
                }
                if (Business.BannerService.Banner_GetByAll().FindAll(b => b.BType == "2" && b.BStatus == "1").Count > 0)
                {
                    imgBanner2 = Business.BannerService.Banner_GetByAll().FindAll(b => b.BType == "3" && b.BStatus == "1")[0].BLink;
                }
                if (Business.BannerService.Banner_GetByAll().FindAll(b => b.BType == "2" && b.BStatus == "1").Count > 0)
                {
                    imgBanner3 = Business.BannerService.Banner_GetByAll().FindAll(b => b.BType == "4" && b.BStatus == "1")[0].BLink;
                }

                this.LoadData();
                LoadNews();
                LoadDataHot();
                LoadSlide();
                //LoadRepeater();
            }
        }
        //Load Slide
        private void LoadSlide()
        {
            rptSlide.DataSource = Business.BannerService.Banner_GetByAll().FindAll(b => b.BStatus == "1" && b.BType == "1");
            rptSlide.DataBind();
        }
        //DetailDelivery
        private int CountDelivery(string PId)
        {
            return Business.DeliveryDetailService.DeliveryDetail_GetByAll().FindAll(d => d.ProductId == PId).Count;
        }
        //Laod data
        private void LoadDataHot()
        {
            List<Data.ProductsInfo> listHot = Business.ProductsService.Products_GetByAll().FindAll(p => this.CountDelivery(p.PId) >= 1).OrderByDescending(p => this.CountDelivery(p.PId)).ToList<Data.ProductsInfo>();
            if (listHot.Count > 9)
            {
                for (int i = 9; i < listHot.Count; i++)
                    listHot.Remove(listHot[i]);
            }
            rptProducts_Banchay.DataSource = listHot;
            rptProducts_Banchay.DataBind();
        }
        private void LoadData()
        {
            List<Data.ProductsInfo> listNew = Business.ProductsService.Products_GetByAll().FindAll(p => p.PActive == "1" && p.PCreateDate != "").OrderByDescending(p => Convert.ToDateTime(p.PCreateDate)).ToList<Data.ProductsInfo>();
            if (listNew.Count > 9)
            {
                for (int i = 9; i < listNew.Count; i++)
                    listNew.Remove(listNew[i]);
            }
            rpt_ProductsNew.DataSource = listNew;
           
           
            rpt_ProductsNew.DataBind();
        }
        //Load cấp cha đầu tiên
       
        //Load News top 5

        private void LoadNews()
        {
            rpNews.DataSource = Business.NewsService.News_GetByTop("5", "", "");
            rpNews.DataBind();
        }
    }
}